﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kTudu.Web.Factories;
using kTudu.Web.Models;
using kTudu.Web.Definitions;
using kTudu.Web.Infraestructure;
using kTudu.Web.Extensions;
using System.Web.Security;

namespace kTudu.Web.Controllers.Base
{
    public class BaseController : Controller
    {
        #region Propiedades

        protected ApplicationFactory _applicationFactory;

        ///// <summary>
        ///// Usuario logueado
        ///// </summary>
        //public UserClient CurrentUser
        //{
        //    get
        //    {
        //        var info = new UserClient();

        //        if (Session != null && Session[Constants.USER_SESSION] is UserClient)
        //        {
        //            info = Session[Constants.USER_SESSION] as UserClient;
        //        }

        //        return info;
        //    }
        //    set
        //    {
        //        Session[Constants.USER_SESSION] = value;
        //    }
        //}

        /// <summary>
        /// Usuario logueado
        /// </summary>
        public UserClient CurrentUser
        {
            get
            {
                var info = new UserClient();

                if (Session != null && Session[Constants.USER_SESSION] is UserClient)
                {
                    info = Session[Constants.USER_SESSION] as UserClient;
                }

                return info;
            }
            set
            {
                Session[Constants.USER_SESSION] = value;
            }
        }

        #endregion

        #region Constructores

        public BaseController()
        {
            _applicationFactory = new ApplicationFactory();
        }

        #endregion

        #region Eventos
        
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (CurrentUser.IdUserClient > 0)
            {
                ViewBag.CurrentUserName = string.Format("{0}, {1}", CurrentUser.Name, CurrentUser.Lastname);
            }
        }

        #endregion

        #region Metodos

        protected virtual void SetViewBag()
        {
            // dummy
        }

        #endregion

        #region Try

        protected virtual ViewResult TryViewResult(Func<ViewResult> action)
        {
            ViewResult result = null;

            try
            {
                Try.It(() => { result = action(); });

                return result;
            }
            catch
            {
                CatchProccess();

                string view = action.Method.GetMethodName();
                return View(view);
            }
        }

        protected virtual ActionResult TryActionResult(Func<ActionResult> action)
        {
            ActionResult result = null;

            try
            {
                Try.It(() => { result = action(); });

                return result;
            }
            catch
            {
                CatchProccess();

                string view = action.Method.GetMethodName();
                return View(view);
            }
        }

        protected virtual void CatchProccess()
        {
            ModelState.AddModelError("", "Se ha producido un error en la aplicación.");
        }
        
        #endregion

    }
}